//
//  LoginViewController.m
//  DriftDog
//
//  Created by rimi on 15/9/7.
//  Copyright (c) 2015年 廖马林. All rights reserved.
//

#import "LoginViewController.h"
#import "QuizView.h"

static NSString *const kanimationDog = @"kanimationDog";
static NSString *const kanimationWithGradientLayer = @"kanimationWithGradientLayer";

@interface LoginViewController ()

@property (nonatomic, strong) UIView *loginView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UIImageView *dog;
@property (nonatomic, strong) QuizView *quizView;
@property (nonatomic, strong) CAGradientLayer *gradientLayer;

- (void)initializeAppearence;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeAppearence];
}

#pragma mark
#pragma mark - init
- (void)initializeAppearence {
    
    [self.view addSubview:self.loginView];
    [self.view.layer addSublayer:self.gradientLayer];
    [self.view addSubview:self.nameLabel];
    [self.view addSubview:self.dog];
    [self.view addSubview:self.quizView];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        
        //标题上移
        [UIView animateWithDuration:2.0 animations:^{
            self.nameLabel.frame = CGRectOffset(self.nameLabel.frame, 0, - 400);
        }];
        
        [self animationWithGradientLayer];
        
        //狗上移
        [UIView animateWithDuration:2.0 animations:^{
            self.dog.frame = CGRectOffset(self.dog.frame, 0, - 302);
        } completion:^(BOOL finished) {
            self.quizView.hidden = NO;
            
        }];
        
        
    });
    
    
}
#pragma mark 
#pragma mark - private
- (void)animationWithGradientLayer {
    
    // gradientLayer动画
    CABasicAnimation * gradientAnimation = [CABasicAnimation animation];
    gradientAnimation.keyPath = @"colors";
    gradientAnimation.duration = 2;
    gradientAnimation.fromValue = @[(__bridge id)[UIColor colorWithRed:255.f/255 green:255.f/255 blue:255.f/255 alpha:1].CGColor, (__bridge id)[UIColor colorWithRed:255.f/255 green:255.f/255 blue:255.f/255 alpha:1].CGColor];
    gradientAnimation.toValue = @[(__bridge id)[UIColor colorWithRed:180.f/255 green:217.f/255 blue:255.f/255 alpha:1].CGColor, (__bridge id)[UIColor colorWithRed:180.f/255 green:217.f/255 blue:255.f/255 alpha:1].CGColor];
    [self.gradientLayer addAnimation:gradientAnimation forKey:kanimationWithGradientLayer];
    
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    
    CAAnimation *animation = [self.gradientLayer animationForKey:kanimationWithGradientLayer];
    if (animation == anim) {
        
        [self.dog removeFromSuperview];
    }
}
#pragma mark 
#pragma mark - getter
- (UIView *)loginView {

    if (!_loginView) {
        _loginView = ({
        
            UIView *view = [[UIView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(0, 0, ORIGIN_WIDTH, ORIGIN_HEIGHT), YES)];
            view.backgroundColor = [UIColor whiteColor];
            
            view;
        });
    }
    return _loginView;
}
- (UILabel *)nameLabel {

    if (!_nameLabel) {
        _nameLabel = ({
        
            UILabel *label = [[UILabel alloc] initWithFrame:DHFlexibleFrame(CGRectMake(ORIGIN_WIDTH / 2 - 175, ORIGIN_HEIGHT / 2 - 250, 350, 200), YES)];
            label.text = @"流浪狗\nStrayDog";
            label.numberOfLines = HUGE_VALF;
            label.font = [UIFont systemFontOfSize:50 * DHFlexibleVerticalMutiplier()];
            label.textColor = [UIColor blackColor];
            label.textAlignment = NSTextAlignmentCenter;
            
            label;
        });
    }
    return _nameLabel;
}
- (UIImageView *)dog {

    if (!_dog) {
        _dog = ({
        
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(ORIGIN_WIDTH / 2 - 41, ORIGIN_HEIGHT / 2 + 13, 83, 175), YES)];
        
            imageView.image = [UIImage imageNamed:@"礼物小狗"];
            
            imageView;
        });
    }
    return _dog;
}
- (QuizView *)quizView {

    if (!_quizView) {
        _quizView = [[QuizView alloc] init];
        _quizView.hidden = YES;
    }
    return _quizView;
}
- (CAGradientLayer *)gradientLayer
{
    if (!_gradientLayer) {
        _gradientLayer = ({
            
            CAGradientLayer * layer = [CAGradientLayer layer];
            
            layer.frame = self.view.bounds;
            layer.colors = @[(__bridge id)[UIColor colorWithRed:255.f/255 green:255.f/255 blue:255.f/255 alpha:1].CGColor, (__bridge id)[UIColor colorWithRed:255.f/255 green:255.f/255 blue:255.f/255 alpha:1].CGColor];
            layer.startPoint = CGPointMake(0, 0);
            layer.endPoint = CGPointMake(0, 1);
            
            layer;
            
        });
    }
    return _gradientLayer;
}

@end
